﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.RPP.EntidadesNegocio;

namespace MJM.RPP.AccesoDatos
{
    public static class HorarioAD
    {
        private static string CambiarFormatoFecha(string fecha)
        {
            return fecha.Substring(6, 2) + "/" + fecha.Substring(4, 2) + "/" + fecha.Substring(0, 4);
        }

        private static string DiaSemana(string fecha)
        {
            DateTime dt = new DateTime(Convert.ToInt32(fecha.Substring(0, 4)), Convert.ToInt32(fecha.Substring(4, 2)), Convert.ToInt32(fecha.Substring(6, 2)));
            string dia = dt.DayOfWeek.ToString();
            if (dia == "Monday")
                return "Lunes";
            if (dia == "Tuesday")
                return "Martes";
            if (dia == "Wednesday")
                return "Miércoles";
            if (dia == "Thursday")
                return "Jueves";
            if (dia == "Friday")
                return "Viernes";
            if (dia == "Saturday")
                return "Sábado";
            if (dia == "Sunday")
                return "Domingo";
            return dia;
        }

        public static List<HorarioEN> ListarHorario(int Empleado,string inicio,string fin)
        {           
            List<HorarioEN> lst = new List<HorarioEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaAsistencia());
            SqlCommand cmd = new SqlCommand("sp_s_T_HORA_EMPL_ProcesarHorario", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vEMP_inCODID", Empleado));
                cmd.Parameters.Add(new SqlParameter("@vDATA_chFECINI", inicio));
                cmd.Parameters.Add(new SqlParameter("@vDATA_chFECFIN", fin));
                cmd.Connection.Open();
                SqlDataAdapter da=new SqlDataAdapter();
                DataTable dt=new DataTable();
                da.SelectCommand = cmd;
                da.Fill(dt);
                int total = dt.Rows.Count;
                for (int i = 0; i < total;i++)
                {
                    var obj = new HorarioEN();
                    obj.DNI = dt.Rows[i][1].ToString();
                    obj.Nombre = dt.Rows[i][2].ToString();
                    obj.Dia = DiaSemana(dt.Rows[i][3].ToString());
                    obj.Fecha = CambiarFormatoFecha(dt.Rows[i][3].ToString());
                    try
                    {
                        obj.HoraIngreso = dt.Rows[i][4].ToString().Substring(0,5);
                    }
                    catch
                    {
                        obj.HoraIngreso = "";
                    }
                    try
                    {
                        obj.HoraSalida = dt.Rows[i][6].ToString().Substring(0,5);
                    }
                    catch
                    {
                        obj.HoraSalida = "";
                    }
                    try
                    {
                        obj.MarcoEntrada = dt.Rows[i][13].ToString();
                    }
                    catch
                    {
                        obj.MarcoEntrada = "";
                    }
                    try
                    {
                        obj.MarcoSalida = dt.Rows[i][14].ToString();
                    }
                    catch
                    {
                        obj.MarcoSalida = "";
                    }

                    obj.Estado = dt.Rows[i][18].ToString();
                    obj.Color = "";
                    if (obj.Estado == "F")
                        obj.Color = "error";
                    if (obj.Estado == "T")
                        obj.Color = "warning";
                    
                    try
                    {
                        obj.Minutos = dt.Rows[i][15].ToString();
                    }
                    catch
                    {
                        obj.Minutos = "";
                    }
                    lst.Insert(0,obj);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();              
            }
            return lst;
        }
    }
}
